package net.tmaize.dao.impl;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import net.tmaize.dao.DetailDao;
import net.tmaize.entity.News_detail;
import net.tmaize.utils.ApiUtils;
import net.tmaize.utils.JsonUtils;
import net.tmaize.utils.UrlUtils;

public class DetailDaoImpl implements DetailDao {

	private String jsonStr = null;

	@Override
	public News_detail getDetail(String id) throws JSONException, IOException {
		
		jsonStr = JsonUtils.getJsonStr(ApiUtils.detailURL + id);
		JSONTokener jsonTokener = new JSONTokener(jsonStr);
		JSONObject jsonObject = new JSONObject(jsonTokener);
		String body = jsonObject.getString("body");
		String image_source = jsonObject.getString("image_source");
		String title = jsonObject.getString("title");
		String image = jsonObject.getString("image");
		String share_url = jsonObject.getString("share_url");
		String thumbnail = jsonObject.getString("thumbnail");
		String ga_prefix = jsonObject.getString("ga_prefix");
		String id_1 = jsonObject.getString("id");
		String css = jsonObject.getJSONArray("css").getString(0);
		News_detail detail = new News_detail(body, image_source, title, UrlUtils.imageUrlChange_1(image), share_url,UrlUtils.imageUrlChange_1(thumbnail), ga_prefix, id_1, css);
		return detail;
	}

	@Override
	public String getJsonStr() {
		return jsonStr;
	}

}
